<?php

namespace app\admin\service;

use app\common\Model\UserVip;

class UserVipService
{
    //成为vip ,若以前是则修改,不是则新增
    public static function alterVipTypeByUids($uids, $start_time, $end_time)
    {
        $UserVip = new UserVip();
        $updateUids = $UserVip->where('uid','in',$uids)->column('uid');
        $createUids = array_diff($uids, $updateUids);

        $updateData = [
            'status' => 1 ,
            'start_time' => $start_time ,
            'end_time' => $end_time ,
        ];
        foreach ($createUids as $v) {
            $createData[] = [
                'uid' => $v,
                'status' => 1 ,
                'start_time' => $start_time ,
                'end_time' => $end_time ,
            ];
        }

        //修改vip状态
        if ($updateUids) {
            $UserVip->where('uid', 'in', $updateUids)
                ->update($updateData);
        }
        //新建vip
        if ($createUids) {
            $UserVip->saveAll($createData);
        }
    }
}